package com.guigu.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.common.utils.R;
import com.guigu.eduservice.entity.Equipment;
import com.guigu.eduservice.entity.EquipmentReservation;
import com.guigu.eduservice.entity.Order;
import com.guigu.eduservice.service.EquipmentReservationService;
import com.guigu.eduservice.service.EquipmentService;
import com.guigu.eduservice.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/advertisement/equipmentReservation")
public class EquipmentReservationController {
    @Autowired
    private EquipmentReservationService equipmentReservationService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private EquipmentService equipmentService;
    //添加设备预定
    @PostMapping("/add")
    public R add(@RequestBody EquipmentReservation equipmentReservation){
        System.out.println("添加设备");
        boolean save = equipmentReservationService.save(equipmentReservation);
        //根据设备id和用户id查询主键
        QueryWrapper<EquipmentReservation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",equipmentReservation.getUserId());
        queryWrapper.eq("equipment_id",equipmentReservation.getEquipmentId());
        queryWrapper.eq("reservation_status",0);
        EquipmentReservation one = equipmentReservationService.getOne(queryWrapper);
        if (save){
            return R.ok().data("reservationId",one.getReservationId());
        }else {
            return R.error();
        }
    }
    //查询是否存在
    @PostMapping("/isEquipmentReservation")
    public String isEquipmentReservation(@RequestBody EquipmentReservation equipmentReservation){
        System.out.println("进入设备预定"+equipmentReservation);
        QueryWrapper<EquipmentReservation> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("user_id",equipmentReservation.getUserId());
        queryWrapper3.eq("equipment_id",equipmentReservation.getEquipmentId());
        queryWrapper3.eq("reservation_status",0);
        EquipmentReservation one1 = equipmentReservationService.getOne(queryWrapper3);
        if (one1 != null){
            return "已存在";
        }else {
        QueryWrapper<EquipmentReservation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("equipment_id",equipmentReservation.getEquipmentId());
        EquipmentReservation one = equipmentReservationService.getOne(queryWrapper);
        if (one != null){
            //根据elementId删除
            boolean removeById = equipmentReservationService.removeById(one);
            //根据elementId删除订单表
            QueryWrapper<Order> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("equipment_id",one.getEquipmentId());
            boolean remove = orderService.remove(queryWrapper1);
            return removeById ? "删除成功" : "删除失败";
        }else {
            return "不存在";
        }
         }
    }
    //查询所有设备预定
    @GetMapping("/selectAll")
    public R selectAll(){
        System.out.println("查询所有设备预定是否过期");
        //需要判断时间是否过期
        List<EquipmentReservation> list = equipmentReservationService.selectAllByUserId();
        return R.ok().data("list",list);
    }
}
